//
//  BTEFacebookManager.swift
//  BTE
//
//  Created by Mike Zhang on 2025/8/5.
//

import UIKit
import FBSDKCoreKit

/// Facebook 配置数据模型
class FacebookConfiguration: NSObject {
    
    @objc var urlSchemeSuffix: String
    @objc var appID: String
    @objc var displayName: String
    @objc var clientToken: String
    
    init(
        urlSchemeSuffix: String,
        appID: String,
        displayName: String,
        clientToken: String
    ) {
        self.urlSchemeSuffix = urlSchemeSuffix
        self.appID = appID
        self.displayName = displayName
        self.clientToken = clientToken
    }
    
}

/// Facebook SDK 管理类
class FacebookSDKManager: NSObject {
    
    static let shared = FacebookSDKManager()
    
    private override init() {}
    
    /// 初始化并配置 Facebook SDK
    func configure(with config: FacebookConfiguration) {
        // 获取 AppDelegate
        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
            return
        }
        
        // 配置 Facebook 设置
        Settings.shared.appID = config.appID
        Settings.shared.clientToken = config.clientToken
        Settings.shared.displayName = config.displayName
        Settings.shared.appURLSchemeSuffix = config.urlSchemeSuffix
        
        // 初始化 SDK
        ApplicationDelegate.shared.application(
            UIApplication.shared,
            didFinishLaunchingWithOptions: appDelegate.launchOptions
        )
  
    }
    
    /// 处理 URL 回调
    func handleOpenURL(_ app: UIApplication, url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
        return ApplicationDelegate.shared.application(
            app,
            open: url,
            sourceApplication: options[.sourceApplication] as? String,
            annotation: options[.annotation]
        )
    }
    
}
